﻿using System;
using System.Linq;
using System.Linq.Expressions;
using System.Linq.Dynamic;using System.Text;
using System.Data.Linq;
using System.Transactions;
using LinqToSqlExtensions;using System.ServiceModel;
using System.Collections;
using System.Collections.Generic;
using CardsServer.DAL;

namespace CardsServer.WCF
{
	public partial interface IService
{
[OperationContract]
string numberpool_Add(numberpool model ,int ACID);


[OperationContract]
string numberpool_Update(numberpool model ,int ACID);


[OperationContract]
string numberpoolByphone_Delete(string phone,int ACID);


[OperationContract]
numberpool numberpoolByphone_Get(string phone,int ACID );


[OperationContract]
List<numberpool> numberpool_Get(string sortBy,int ACID);




}
public partial class Service : IService
{
#region  添加numberpool表数据
/// <summary>
/// 添加numberpool
/// </summary>
/// <param name="model">numberpool实体</param>
/// <returns>空或错误</returns>
public string numberpool_Add(numberpool model,int ACID)
{
if(string.IsNullOrEmpty(model.phone))
{
return "『号码』不能为空！";
}
if(model.balance==null )
{
return "『卡内余额』不能为空！";
}
if(model.state==null )
{
return "『状态0=未使用 1=使用中 2=停机』不能为空！";
}
if(model.indate==null )
{
return "『购入日期』不能为空！";
}
if(model.ComboID==null )
{
return "『套餐ID』不能为空！";
}
using(CardsServer.DAL.DataClassesDataContext ctx=new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
{
try
{
if(ctx.numberpool.Count(p=>p.phone==model.phone)>0)
{
return "主键已存在！";
}
ctx.numberpool.InsertOnSubmit(model);
ctx.SubmitChanges();
return string.Empty;
}
catch (Exception ex)
{
return " 添加失败!错误详细信息：" + ex.Message;
}
}
}
#endregion

#region  添加numberpool表数据事物用重构
/// <summary>
/// 添加numberpool事物用重构
/// </summary>
/// <param name="model">numberpool实体</param>
/// <returns>空或错误</returns>
private string numberpool_Add(numberpool model,int ACID, CardsServer.DAL.DataClassesDataContext ctx)
{
if(string.IsNullOrEmpty(model.phone))
{
return "『号码』不能为空！";
}
if(model.balance==null )
{
return "『卡内余额』不能为空！";
}
if(model.state==null )
{
return "『状态0=未使用 1=使用中 2=停机』不能为空！";
}
if(model.indate==null )
{
return "『购入日期』不能为空！";
}
if(model.ComboID==null )
{
return "『套餐ID』不能为空！";
}
if(ctx.numberpool.Count(p=>p.phone==model.phone)>0)
{
    return string.Format("号码{0}已存在于号码池！请核查号码。",model.phone);
}
ctx.numberpool.InsertOnSubmit(model);
return string.Empty;
}
#endregion



#region  修改numberpool表数据
/// <summary>
/// 修改numberpool
/// </summary>
/// <param name="model">numberpool实体</param>
/// <returns>空或错误</returns>
public string numberpool_Update(numberpool model,int ACID)
{
if(string.IsNullOrEmpty(model.phone))
{
return "『号码』不能为空！";
}
if(model.balance==null )
{
return "『卡内余额』不能为空！";
}
if(model.state==null )
{
return "『状态0=未使用 1=使用中 2=停机』不能为空！";
}
if(model.indate==null )
{
return "『购入日期』不能为空！";
}
if(model.ComboID==null )
{
return "『套餐ID』不能为空！";
}
using(CardsServer.DAL.DataClassesDataContext ctx=new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
{
try
{
if(ctx.numberpool.Count(p=>p.phone==model.phone)!= 1)
{
return "未找到指定记录或记录不唯一！";
}
numberpool numberpoolmodel=ctx.numberpool.Single(p => p.phone==model.phone);
if(!string.IsNullOrEmpty(model.phone))
{
numberpoolmodel.phone=model.phone;
}
if(!string.IsNullOrEmpty(model.cno))
{
numberpoolmodel.cno=model.cno;
}
if(model.ComboID!=null )
{
numberpoolmodel.ComboID=model.ComboID;
}
if(model.indate!=null )
{
numberpoolmodel.indate=model.indate;
}
if(model.state!=null )
{
numberpoolmodel.state=model.state;
}
if(model.balance!=null )
{
numberpoolmodel.balance=model.balance;
}
if(model.expense!=null )
{
numberpoolmodel.expense=model.expense;
}
if(model.activedate!=null )
{
numberpoolmodel.activedate=model.activedate;
}
if(model.exceeddate!=null )
{
numberpoolmodel.exceeddate=model.exceeddate;
}
if (model.exceeddate != null)
{
    numberpoolmodel.exceeddate = model.exceeddate;
}

    numberpoolmodel.username = model.username;
    numberpoolmodel.pwd = model.pwd;
    numberpoolmodel.Remarks = model.Remarks;
    numberpoolmodel.userIDcard = model.userIDcard;
    numberpoolmodel.htno = model.htno;
    numberpoolmodel.linktel = model.linktel;
ctx.SubmitChanges();
return string.Empty;
}
catch (Exception ex)
{
return " 修改失败!错误详细信息：" + ex.Message;
}
}
}
#endregion



#region  删除numberpool表Byphone数据
/// <summary>
/// 删除numberpoolByphone
/// </summary>
/// <param name="phone">号码</param>
/// <returns>空或错误</returns>
public string numberpoolByphone_Delete(string phone,int ACID)
{
if(string.IsNullOrEmpty(phone))
{
return "『号码』不能为空！";
}
using(CardsServer.DAL.DataClassesDataContext ctx=new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
{
try
{
if(ctx.numberpool.Count(p=>p.phone==phone)!=1)
{
return null;
}
numberpool model=ctx.numberpool.Single(p => p.phone== phone);
ctx.numberpool.DeleteOnSubmit(model);
ctx.SubmitChanges();
return string.Empty;
}
catch (Exception ex)
{
return " 删除失败!错误详细信息：" + ex.Message;
}
}
}
#endregion

#region  删除numberpool表Byphone数据[事物用重构]
/// <summary>
/// 删除numberpoolByphone[事物用重构]
/// </summary>
/// <param name="phone">号码</param>
/// <returns>空或错误</returns>
private string numberpoolByphone_Delete(string phone,int ACID, CardsServer.DAL.DataClassesDataContext ctx)
{
if(string.IsNullOrEmpty(phone))
{
return "『号码』不能为空！";
}
if(ctx.numberpool.Count(p=>p.phone==phone)!=1)
{
return null;
}
numberpool model=ctx.numberpool.Single(p => p.phone== phone);
ctx.numberpool.DeleteOnSubmit(model);
return string.Empty;
}
#endregion



#region  获取numberpool表Byphone数据
/// <summary>
/// 获取numberpoolByphone
/// </summary>
/// <param name="phone">号码</param>
/// <returns>当前表</returns>
public numberpool numberpoolByphone_Get(string phone,int ACID)
{
if(string.IsNullOrEmpty(phone))
{
return null;
}
using(CardsServer.DAL.DataClassesDataContext ctx=new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
{
try
{
if(ctx.numberpool.Count(p=>p.phone==phone)!=1)
{
return null;
}
return ctx.numberpool.SingleOrDefault(p=>p.phone==phone);
}
catch (Exception ex)
{
return null;
}
}
}
#endregion



#region  获取numberpool表数据
/// <summary>
/// 获取numberpool
/// </summary>
/// <param name="sortBy">排序属性名</param>
/// <param name="sortType">排序类型0=升序 1=降序</param>
/// <returns>List<numberpool></returns>
public List<numberpool> numberpool_Get(Expression<Func<numberpool, bool>> predicate , string sortBy,int ACID)
{
using(CardsServer.DAL.DataClassesDataContext ctx=new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
{
try
{
if (predicate == null)
{
if(string.IsNullOrWhiteSpace(sortBy))
{
return ctx.numberpool.ToList();
}
 else 
{
return ctx.numberpool.OrderBy(sortBy).ToList();
}
}
else
{
if(string.IsNullOrWhiteSpace(sortBy))
{
return ctx.numberpool.Where(predicate).ToList();
}
 else 
{
return ctx.numberpool.Where(predicate).OrderBy(sortBy).ToList();
}
}
}
catch (Exception ex)
{
return null;
}
}
}
#endregion


#region  获取numberpool表数据
/// <summary>
/// 获取numberpool
/// </summary>
/// <param name="sortBy">排序属性名</param>
/// <param name="sortType">排序类型0=升序 1=降序</param>
/// <returns>List<numberpool></returns>
public List<numberpool> numberpool_Get(string sortBy,int ACID)
{
using(CardsServer.DAL.DataClassesDataContext ctx=new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
{
try
{
return numberpool_Get(null,sortBy, ACID);
}
catch (Exception)
{
return null;
}
}
}
#endregion


#region  获取numberpool表数据计数
/// <summary>
/// 获取numberpool表数据计数
/// </summary>
/// <returns>List<numberpool></returns>
public int numberpool_GetCount(Expression<Func<numberpool, bool>> predicate,int ACID)
{
using(CardsServer.DAL.DataClassesDataContext ctx=new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
{
try
{
if (predicate == null)
{
return ctx.numberpool.Count();

}
 else
{
return ctx.numberpool.Where(predicate).Count();
}
}
catch 
{
return 0;
}
}
}
#endregion




#region  获取numberpool分页表数据
/// <summary>
/// 获取numberpool分页表数据
/// </summary>
/// <param name="pageIndex">当前页</param>
/// <param name="pageSize">页大小</param>
/// <param name="sortBy">排序属性名</param>
/// <param name="sortType">排序类型0=升序 1=降序</param>
/// <returns>List<numberpool></returns>
public List<numberpool> numberpool_Getpage(int pageIndex, int pageSize,Expression<Func<numberpool, bool>> predicate ,string sortBy,int ACID)
{
using(CardsServer.DAL.DataClassesDataContext ctx=new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
{
try
{
if (predicate == null)
{
if(string.IsNullOrWhiteSpace(sortBy))
{
return ctx.numberpool.Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();
}
 else 
{
return ctx.numberpool.OrderBy(sortBy).Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();
}
}
else
{
if(string.IsNullOrWhiteSpace(sortBy))
{
return ctx.numberpool.Where(predicate).Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();
}
 else 
{
return ctx.numberpool.Where(predicate).OrderBy(sortBy).Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();
}
}
}
catch (Exception ex)
{
return null;
}
}
}
#endregion


#region  批量修改numberpool表数据
/// <summary>
/// 批量修改numberpool
/// </summary>
/// <param name="predicate">Expression<Func<numberpool, bool>></param> 查找表达式树
/// <param name="updater">Expression<Func<numberpool,numberpool>> 更新表达式树</param>
/// <param name="returns">影响记录数</param>
public int numberpool_batchUpdate(Expression<Func<numberpool, bool>> predicate,Expression<Func<numberpool, numberpool>> updater,int ACID)
{
using (TransactionScope ts=new TransactionScope())
{
using(CardsServer.DAL.DataClassesDataContext ctx=new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
{
try
{
int ok = ctx.numberpool.Update(predicate, updater);
ts.Complete();
 return ok;
}
catch (Exception ex)
{
return 0;
}
}
}
}
#endregion


#region  批量修改numberpool表数据事物用重构
/// <summary>
/// 批量修改numberpool事物用重构
/// </summary>
/// <param name="predicate">Expression<Func<numberpool, bool>></param> 查找表达式树
/// <param name="updater">Expression<Func<numberpool,numberpool>> 更新表达式树</param>
/// <param name="ctx">数据上下文</param>
/// <param name="returns">影响记录数</param>
private int numberpool_batchUpdate(Expression<Func<numberpool, bool>> predicate,Expression<Func<numberpool, numberpool>> updater,int ACID, CardsServer.DAL.DataClassesDataContext ctx)
{
int ok = ctx.numberpool.Update(predicate, updater);
return ok;
}
#endregion


#region  批量删除numberpool表数据
/// <summary>
/// 批量删除numberpool
/// </summary>
/// <param name="predicate">Expression<Func<numberpool, bool>></param> 查找表达式树
/// <param name="returns">影响记录数</param>
public int numberpool_batchDel(Expression<Func<numberpool, bool>> predicate,int ACID)
{
using (TransactionScope ts=new TransactionScope())
{
using(CardsServer.DAL.DataClassesDataContext ctx=new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
{
try
{
int ok = ctx.numberpool.Delete(predicate);
ts.Complete();
 return ok;
}
catch (Exception ex)
{
return 0;
}
}
}
}
#endregion


#region  批量删除numberpool表数据事物用重构
/// <summary>
/// 批量删除numberpool事物用重构
/// </summary>
/// <param name="predicate">Expression<Func<numberpool, bool>></param> 查找表达式树
/// <param name="ctx">数据上下文</param>
/// <param name="returns">影响记录数</param>
private int numberpool_batchDel(Expression<Func<numberpool, bool>> predicate,int ACID, CardsServer.DAL.DataClassesDataContext ctx)
{
int ok = ctx.numberpool.Delete(predicate);
return ok;
}
#endregion


}
}

